package yxy.game.pm2.active.command.flashsale;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import yxy.cherry.data.bean.actsnapup;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_限时抢购;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.FlashSale;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActFlashSaleInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@MessageType(proto = Command.ActFlashSaleInfo, classtype = ActiveMessage.ActFlashSaleInfoRequest.class)
public class CMD_Active_限时抢购_Info extends Command<ActFlashSaleInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActFlashSaleInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActFlashSaleInfoResponse.Builder response = ActiveMessage.ActFlashSaleInfoResponse.newBuilder();
		
		ActiveModule_限时抢购 am = ActiveModule_限时抢购.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		Map<String, actsnapup> aspmap = ActDataCenter.getAllData(actsnapup.class, playercode);
		
		boolean save = false;
		DateTime now = DateTime.now();
		for(FlashSale fs : am.getFss()) {
			ActiveMessage.FlashSale.Builder build = ActiveMessage.FlashSale.newBuilder();
			
			actsnapup data = aspmap.get(String.valueOf(fs.getId()));
			Collection<Currency> curs =  Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			
			Set<String> keys = fs.getOrderidtimemap().keySet();
			for(String key : keys) {
				HashMap<String, DateTime> map = new HashMap<String, DateTime>();
				long times = now.getMillis() - fs.getOrderidtimemap().get(key).getMillis();
				if(times > DateTimeConstants.MILLIS_PER_MINUTE * 5) {
					map.remove(key);
					save = true;
				}
			}

			actsnapup actsnapup = ActDataCenter.getData(fs.getId(), yxy.cherry.data.bean.actsnapup.class, playercode);
			build.setId(fs.getId());
			//build.setGoodsid(fs.getGoodsid());
			build.setGoodsid(actsnapup.getPurchasever());  //商品价格
			build.setBuylimitnum(fs.getBuylimitnum());
			build.setBuynum(fs.getBuynum());
			//build.setPakename(fs.getPakename());
			build.setPakename(actsnapup.getPakename());
			build.addAllCurs(items);
			build.setLocknum(fs.getOrderidtimemap().size());
			build.setRebate(data.getRebate()); //返利倍数

			response.addFss(build.build());
		}
		
		DateTime refreshtime = am.getRefreshtime();
		DateTime roundendtime = refreshtime.plusDays(7);	//本轮结束时间
		
		response.setEnd(roundendtime.getMillis());
		send(messageSource, response, Result.Success);
		
		if(save) {
			am.set();
		}
	}

	private void send(MessageSource messageSource, ActiveMessage.ActFlashSaleInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
//	public static void main(String[] str) {
//		DateTime refreshtime = ActiveUtil.time0();
//		DateTime roundendtime = refreshtime.plusDays(7);	//本轮结束时间
//		System.out.println(refreshtime);
//		System.out.println(roundendtime);
//	}
}
